"""add_end_user_account_joins_extend

Revision ID: 006_end_user_account_joins
Revises: 005_account_money_extend
Create Date: 2024-08-03 07:24:13.552085

"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.engine.reflection import Inspector

from models import db, types

# revision identifiers, used by Alembic.
revision = '006_end_user_account_joins'
down_revision = '005_account_money_extend'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    conn = op.get_bind()
    inspector = Inspector.from_engine(conn)
    tables = inspector.get_table_names()
    
    if 'end_user_account_joins_extend' not in tables:
        op.create_table('end_user_account_joins_extend',
        sa.Column('id', types.StringUUID(), server_default=sa.text('uuid_generate_v4()'), nullable=False),
        sa.Column('end_user_id', types.StringUUID(), nullable=False),
        sa.Column('account_id', types.StringUUID(), nullable=False),
        sa.Column('app_id', types.StringUUID(), nullable=False),
        sa.Column('created_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP(0)'), nullable=False),
        sa.Column('updated_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP(0)'), nullable=False),
        sa.PrimaryKeyConstraint('id', name='end_user_account_joins_pkey')
        )
        with op.batch_alter_table('end_user_account_joins_extend', schema=None) as batch_op:
            batch_op.create_index('end_user_account_joins_account_id_idx', ['account_id'], unique=False)
            batch_op.create_index('end_user_account_joins_end_user_id_app_id_idx', ['end_user_id', 'app_id'], unique=False)
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('end_user_account_joins_extend', schema=None) as batch_op:
        batch_op.drop_index('end_user_account_joins_end_user_id_app_id_idx')
        batch_op.drop_index('end_user_account_joins_account_id_idx')

    op.drop_table('end_user_account_joins_extend')
    # ### end Alembic commands ###
